﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using TheDealmap;
using TheDealmap.Data;
using TheDealmap.Enum;
using TheDealmap.Util;

namespace TheDealMapSample
{
    class Program
    {//"Huston, TX" "Boston, MA"
        //Latitude = 29.982855,
        //                       Longitude = -41.484375
        //static Capability DoStuf(int g)
        //{
        //    Capability c = Capability.Unknown;
        //    if ((g & (int)Capability.Favorite) > 0 )
        //    {
        //        c |= Capability.Favorite;
        //    }
        //}

        static void Main(string[] args)
        {
            Console.WriteLine("Starting.");
            //Andrew's apikey
            Service dealmap = new Service("0-673995604-634157758518545000");

            int v = 83;

            //var stuff = Capability.HasTransaction | Capability.HasTransaction
            //Capability.HasTransaction;




            CFlagsExtracter<Capability> flager = new CFlagsExtracter<Capability>(v);
            var cat = flager.aeFlags;
            var junk = Capability.Unknown;

            var flunk = cat.Aggregate((a, b) => a |= b);
            foreach (var capability in cat)
            {
                junk |= capability;
            }

            var bunk = junk;
            var yonl = flunk;

            var moredeals = Service.SearchDeals("0-673995604-634157758518545000", CenterdActivity.Casual,
                                                Capability.FiftyPercentOrMore, DateTime.Now.AddDays(10), 40.7143553,
                                                -74.005973, null, null, null, null);

            var crunk = moredeals.Matched[0].Activity;

            var brunk = moredeals.Matched[0].Capability;
            Console.WriteLine(String.Format("Number of deals returned: {0}", moredeals.TotalResults));

            var businesses = dealmap.SearchBusinessListings(
                CenterdActivity.Unknown,
                47.67856,
                -122.130994,
                "Cool",
                40.3,
                4,
                100
                );
            Console.WriteLine(String.Format("Number of Businesses Found: {0}", businesses.TotalResults));



            var deal = new ProposedDeal()
                           {
                               Restrictions = "DEV",
                               Title = "DEV",
                               EffectiveDate = DateTime.Now,
                               ExpirationDate = DateTime.Now.AddDays(3.0),
                               AddedBy = "probablycorey@moo.com",
                               Country = "USA",
                               City = "Cupertino",
                               State = "CA",
                               BusinessName = "A-124-7 Locksmith",
                               StreetAddress = "20630Valley Green Dr",
                               Latitude = 29.98285500000003003,
                               Longitude = -41.48437500030030303,
                               Category = "HomeGarden",
                               CenterdActivity = CenterdActivity.Romantic | CenterdActivity.Fun
                               
                           };
            var success = dealmap.SubmitDeal(deal);
            Console.WriteLine(String.Format("Return code: {0} reason {1}", success.Status, success.Message));

            var deals = dealmap.GetDealDetails("0-1uWtMdXD-385027230");
            Console.WriteLine(deals);

            Console.WriteLine("Finished!");
            Console.ReadLine();

        }

        //static void Main(string[] args)
        //{
        //    ////Deal Methods

        //    //////Search Deals
        //    ////Console.WriteLine("Search:");
        //  //  DoGet("http://api.thedealmap.com/search/deals/?l=seattle");

        //    //////Get a deal
        //    //Console.WriteLine("Get Deal:");
        //    DoGet("http://api.thedealmap.com/deals/0-1uWtMdXD-385027230");

        //    //////Add Deal
        //    ////Console.WriteLine("Add Deal:");
        //    //DoPost("http://api.thedealmap.com/deals/",
        //    //"<AddDealRequest xmlns=\"http://thedealmap.com/api\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><ID></ID><Title>DEV</Title><Details/><Restrictions>DEV</Restrictions><EffectiveDate>2010-06-10T23:06:01</EffectiveDate><ExpirationDate>2010-06-11T23:06:01</ExpirationDate><Url/><IsExclusive>0</IsExclusive><FirstName /><LastName/><IsOwner>false</IsOwner><AddedBy>probablycorey@moo.com</AddedBy><SocailNetworkID>100000136203978</SocailNetworkID><DailyDealsEmailOK>false</DailyDealsEmailOK><StreetAddress>20630Valley Green Dr</StreetAddress><Latitude>0</Latitude><Longitude>0</Longitude><City>Cupertino</City><State>CA</State><Country>USA</Country><BusinessName>A-124-7 Locksmith</BusinessName><BusinessDetails /><PhotoType/><PhotoContent /><Tags/><Category>HomeGarden</Category><Styles>0</Styles><VendorId/><DealSource>0</DealSource></AddDealRequest>");

        //    /////////Deal Comments
        //    ////////Post a comment
        //    //DoPost("http://api.thedealmap.com/deals/0-1uWtMdXD-385027230/comments",
        //    //    "<AddCommentRequest xmlns=\"http://thedealmap.com/api\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><UserID>509679857</UserID><Comment>Awesome deal!</Comment></AddCommentRequest>");

        //    ////////Get deal comments
        //    ////////Url = /Services/Deals/{dealID}/Comments
        //    //DoGet("http://api.thedealmap.com/deals/0-1uWtMdXD-385027230/comments");

        //    //////User Methods
        //    //////Add User
        //    //////Url = /Services/DealMapWebService.svc/Users/{userID}
        //    //DoPost("http://api.thedealmap.com/users/100000136203978",
        //    ////    "<UserProfile xmlns=\"http://thedealmap.com/api\" xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><FirstName>Mike</FirstName><LastName>Nash</LastName><EmailAddress>cthota@centerd.com</EmailAddress><City>London</City><State/><Country>UK</Country><PhotoUrl /><Gender>Male</Gender><IsBusinessOwner>false</IsBusinessOwner><DailyDealsEmailOK>false</DailyDealsEmailOK><SocailNetworkID>100000123252336</SocailNetworkID></UserProfile>");
        //    //        "<UserProfile xmlns=\"http://thedealmap.com/api\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAddress>test@centerd.com</EmailAddress><FirstName>Probably</FirstName><LastName>Corey</LastName><City/><State /><Country/><Gender>Male</Gender><IsBusinessOwner>false</IsBusinessOwner><SocailNetworkID>100000136203978</SocailNetworkID><DailyDealsEmailOK>false</DailyDealsEmailOK><PhotoUrl>http://profile.ak.fbcdn.net/v22943/24/54/s100000136203978_4480.jpg</PhotoUrl></UserProfile>");


        //    ////Get User
        //    ////Url = /Services/DealMapWebService.svc/Users/{userID}
        //    //DoGet("http://api.thedealmap.com/users/509679857");

        //    ////Get User Status
        //    ////Url = /Services/DealMapWebService.svc/Users/{userID}/Status
        //    //DoGet("http://api.thedealmap.com/users/509679857/status");

        //    ////Get User Deals
        //    ////Url = /Services/DealMapWebService.svc/Users/{userID}/Deals
        //    //DoGet("http://api.thedealmap.com/users/509679857/deals");

        //    ////Update User Status
        //    ////Url = /Services/Users/{userID}/Status/{action}
        //    //DoPost("http://api.thedealmap.com/users/509679857/status/4", ""); //Deal Add Action

        //    /////Status methods - used for displaying leaderboards
        //    /////
        //    ////Get the top dealmap users
        //    ////Url = /Services/Status
        //    //DoGet("http://api.thedealmap.com/status/");

        //    ////Get the top dealmap users by specific action
        //    ////Url = /Services/Status/action
        //    //DoGet("http://api.thedealmap.com/status/4"); //Top deal submitters

        //    /////Other methods

        //    Console.ReadKey();
        //}

        //static string DoGet(string url)
        //{
        //    System.Net.WebClient wc = new System.Net.WebClient();
        //    byte[] buffer = wc.DownloadData(url);
        //    string data = System.Text.Encoding.UTF8.GetString(buffer);
        //    Console.WriteLine(data);
        //    return data;
        //}


        //static void DoPost(string url, string body)
        //{
        //    if (body != "")
        //    {
        //        //Verify that XML is formed properly.
        //        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        //        doc.LoadXml(body);
        //    }

        //    //System.Net.ServicePointManager.Expect100Continue = false;

        //    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        //    request.AllowAutoRedirect = true;
        //    //request.MaximumAutomaticRedirections = 3;
        //    request.Method = "POST";
        //    byte[] byteArray = Encoding.UTF8.GetBytes(body);
        //    request.ContentLength = byteArray.Length;
        //    request.ContentType = "application/xml; charset=utf-8";
        //    System.IO.Stream dataStream = request.GetRequestStream();
        //    dataStream.Write(byteArray, 0, byteArray.Length);
        //    dataStream.Close();
        //    System.Net.WebResponse response = request.GetResponse();
        //    Console.WriteLine(((System.Net.HttpWebResponse)response).StatusDescription);
        //    dataStream = response.GetResponseStream();
        //    System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
        //    string responseFromServer = reader.ReadToEnd();
        //    Console.WriteLine(responseFromServer);
        //    reader.Close();
        //    dataStream.Close();
        //    response.Close();
        //}
    }
}
